<?php
class Report extends CI_Controller {
	
	public function __construct()
	{
		parent::__construct();
		$this->load->helper('url');
		$this->load->model('crm_model');
	}
	
	public function data_report($page = "report")
	{
		if ( ! file_exists('application/views/crm/'.$page.'.php'))
		{
			show_404();
		}
		
		$company_info = $this->crm_model->get_company_info();
		$this->load->library('PHPExcel');
		$this->load->library('PHPExcel/IOFactory');
		
		// 创建一个处理对象实例      
		$objExcel = new PHPExcel();      
		     
		// 创建文件格式写入对象实例, uncomment      
		 $objWriter = IOFactory::createWriter($objExcel, 'Excel2007'); 
		 
		$objProps = $objExcel->getProperties();      
		$objProps->setCreator("qisou");      
		$objProps->setLastModifiedBy("qisou");      
		$objProps->setTitle("客户信息表");      
		$objProps->setSubject("客户信息表");      
		$objProps->setDescription("客户信息表");      
		$objProps->setKeywords("客户信息表");      
		$objProps->setCategory("客户信息表"); 
		
		//*************************************      
		//设置当前的sheet索引，用于后续的内容操作。      
		//一般只有在使用多个sheet的时候才需要显示调用。      
		//缺省情况下，PHPExcel会自动创建第一个sheet被设置SheetIndex=0      
		$objExcel->setActiveSheetIndex(0);      
		$objActSheet = $objExcel->getActiveSheet(); 
		
		//设置当前活动sheet的名称      
		$objActSheet->setTitle('客户列表');      
		     
		//*************************************      
		//      
		//设置宽度，这个值和EXCEL里的不同，不知道是什么单位，略小于EXCEL中的宽度  
		$objActSheet->getColumnDimension('A')->setWidth(20);   
		$objActSheet->getColumnDimension('B')->setWidth(10);   
		$objActSheet->getColumnDimension('C')->setWidth(20);   
		$objActSheet->getColumnDimension('D')->setWidth(20);   
		$objActSheet->getColumnDimension('E')->setWidth(15);   
		$objActSheet->getColumnDimension('F')->setWidth(15);   
		$objActSheet->getColumnDimension('G')->setWidth(20);   
		$objActSheet->getColumnDimension('H')->setWidth(18);   
		$objActSheet->getColumnDimension('I')->setWidth(20);   
		$objActSheet->getColumnDimension('J')->setWidth(18);   
		$objActSheet->getColumnDimension('K')->setWidth(8);   
   
		  
		$objActSheet->getRowDimension(1)->setRowHeight(30);   
		$objActSheet->getRowDimension(2)->setRowHeight(27);   
		$objActSheet->getRowDimension(3)->setRowHeight(16);   
		   
		//设置单元格的值    
		$objActSheet->setCellValue('A1', '客户信息列表');   
		//合并单元格  
		$objActSheet->mergeCells('A1:K1');   
		//设置样式  
		$objStyleA1 = $objActSheet->getStyle('A1');      
		$objStyleA1->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);  
		$objFontA1 = $objStyleA1->getFont();      
		$objFontA1->setName('宋体');      
		$objFontA1->setSize(18);    
		$objFontA1->setBold(true);      
		  
		//设置居中对齐  
		$objActSheet->getStyle('A2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);  
		$objActSheet->getStyle('B2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);  
		$objActSheet->getStyle('C2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);  
		$objActSheet->getStyle('D2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);  
		$objActSheet->getStyle('E2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);  
		$objActSheet->getStyle('F2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);  
		$objActSheet->getStyle('G2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);  
		$objActSheet->getStyle('H2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);  
		$objActSheet->getStyle('I2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);  
		$objActSheet->getStyle('J2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);  
		$objActSheet->getStyle('K2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);   
		  
		$objActSheet->setCellValue('A2', '客户');   
		$objActSheet->setCellValue('B2', '联系人');   
		$objActSheet->setCellValue('C2', '手机');   
		$objActSheet->setCellValue('D2', '电话');   
		$objActSheet->setCellValue('E2', '传真');   
		$objActSheet->setCellValue('F2', '网址');   
		$objActSheet->setCellValue('G2', '邮箱');   
		$objActSheet->setCellValue('H2', '即时通信');   
		$objActSheet->setCellValue('I2', '地区');   
		$objActSheet->setCellValue('J2', '地址');   
		$objActSheet->setCellValue('K2', '备注');  
		$row = 3;
		foreach ($company_info as $list)
		{
			$objActSheet->setCellValue('A'.$row, $list['company_name']);   
			$objActSheet->setCellValue('B'.$row, $list['contact_person']);   
			$objActSheet->setCellValue('C'.$row, $list['call_phone']);   
			$objActSheet->setCellValue('D'.$row, $list['tel']);   
			$objActSheet->setCellValue('E'.$row, $list['fax']);   
			$objActSheet->setCellValue('F'.$row, $list['company_website']);   
			$objActSheet->setCellValue('G'.$row, $list['email']);   
			$objActSheet->setCellValue('H'.$row, $list['data_sources']);   
			$objActSheet->setCellValue('I'.$row, $list['address']);   
			$objActSheet->setCellValue('J'.$row, $list['address']);   
			$objActSheet->setCellValue('K'.$row, $list['company_id']); 	
			$row++;
		}  
		  
		//设置边框  
		$objActSheet->getStyle('A2')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );  
	    $objActSheet->getStyle('A2')->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );  
	    $objActSheet->getStyle('A2')->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );  
	    $objActSheet->getStyle('A2')->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );  
	    $objActSheet->getStyle('B2')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );  
	    $objActSheet->getStyle('B2')->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );  
	    $objActSheet->getStyle('B2')->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );  
	    $objActSheet->getStyle('B2')->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );  
	    $objActSheet->getStyle('C2')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );  
	    $objActSheet->getStyle('C2')->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );  
	    $objActSheet->getStyle('C2')->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );  
	    $objActSheet->getStyle('C2')->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );  
	    $objActSheet->getStyle('D2')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );  
	    $objActSheet->getStyle('D2')->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );  
	    $objActSheet->getStyle('D2')->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );  
	    $objActSheet->getStyle('D2')->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );  
	    $objActSheet->getStyle('E2')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );  
	    $objActSheet->getStyle('E2')->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );  
	    $objActSheet->getStyle('E2')->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );  
	    $objActSheet->getStyle('E2')->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );  
	    $objActSheet->getStyle('F2')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );  
	    $objActSheet->getStyle('F2')->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );  
	    $objActSheet->getStyle('F2')->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );  
	    $objActSheet->getStyle('F2')->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );  
	    $objActSheet->getStyle('G2')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );  
	    $objActSheet->getStyle('G2')->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );  
	    $objActSheet->getStyle('G2')->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );  
	    $objActSheet->getStyle('G2')->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );  
	    $objActSheet->getStyle('H2')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );  
	    $objActSheet->getStyle('H2')->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );  
	    $objActSheet->getStyle('H2')->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );  
	    $objActSheet->getStyle('H2')->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );  
	    $objActSheet->getStyle('I2')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );  
	    $objActSheet->getStyle('I2')->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );  
	    $objActSheet->getStyle('I2')->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );  
	    $objActSheet->getStyle('I2')->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );  
	    $objActSheet->getStyle('J2')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );  
	    $objActSheet->getStyle('J2')->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );  
	    $objActSheet->getStyle('J2')->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );  
	    $objActSheet->getStyle('J2')->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );  
	    $objActSheet->getStyle('K2')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );  
	    $objActSheet->getStyle('K2')->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );  
	    $objActSheet->getStyle('K2')->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );  
	    $objActSheet->getStyle('K2')->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );   
	    
	     header('Content-Type: application/vnd.ms-excel');
	     header('Content-Disposition: attachment;filename="Products_'.date('dMy').'.xls"');
	     header('Cache-Control: max-age=0');
	     
	     $objWriter->save('php://output');
	}
}